package com.ydgk.ajax;

import com.alibaba.fastjson.JSON;
import com.ydgk.bean.Admin;
import com.ydgk.dao.AdminDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "AdminPagingAjaxServlet", urlPatterns = "/AdminPagingAjaxServlet.action")
public class AdminPagingAjaxServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageNumber = request.getParameter("pageNumber");
        AdminDao adminDao = new AdminDao();
        int page = 0;
        try{
            page = Integer.parseInt(pageNumber);
        }catch (NumberFormatException e){
            page = 1;
        }
        int maxPage = adminDao.maxPage("admin");
        if(page>maxPage){
            page = maxPage;
        }else if(page<0){
            page = 1;
        }
        List<Admin> adminList = adminDao.paging(page);
        Map map = new HashMap();
        map.put("pageNumber",page);
        map.put("maxPage",maxPage);
        map.put("adminList", adminList);
        response.getWriter().print(JSON.toJSONString(map));
    }


}
